<template>
  <view class="ver-layout">
    <view class="top-tabs hor-layout">
      <view v-for="(item,index) in tabList" class="tabs-view" :class="{'select':item.value===selectTabValue}"
            @click="tabClick(item)">
        {{ item.name }}
      </view>
    </view>
    <!--  作业列表  -->
    <view class="homework-list">
      <view v-if="approvalList.length<=0" class="list-no-data">暂无数据</view>
      <block v-else>
        <view class="ask-break hor-layout" :class="{'top':index===0}" v-for="(item,index) in approvalList"
              :key="index" @click="toBreakDetail(item)">
          <image class="ask-break-icon" src="@/static/home/ic_ask_break_oa.png"></image>
          <view class="approval-layout ver-layout">
            <view class="title">{{ item.type }}审批</view>
            <view class="approval-infos ver-layout">
              <!--            <view class="value">审批</view>-->
              <view class="title">{{ item.realname }}</view>
              <view class="detail hor-layout">
                <view>请假类型：</view>
                <view class="text">{{ item.type }}</view>
              </view>
              <view class="detail hor-layout">
                <view>开始时间：</view>
                <view class="text single-line"> {{ item.start_time }}</view>
              </view>
              <view class="detail hor-layout">
                <view>结束时间：</view>
                <view class="text single-line"> {{ item.end_time }}</view>
              </view>
              <view class="value" style="margin-top: 29px">{{ approvalStatus[item.status] }}</view>
            </view>
          </view>
        </view>
        <u-loadmore v-if="loadingObj.status" margin-top="15" :status="loadingObj.status"/>
      </block>
    </view>
  </view>
</template>

<script>
import {breakDetail} from "@/router.js"
import {AskBreakApprovalApi} from "@/api/askBreak.js"

let engine = new AskBreakApprovalApi()
export default {
  name: "askBreak",
  data() {
    return {
      tabList: [{name: '学生', value: 'student'}, {name: '教师', value: 'teacher'}],
      selectTabValue: 'student',
      approvalList: [1, 1, 1],
      approvalStatus: {0: '驳回', 1: '待审', 2: '通过'},
      loadingObj: {
        status: '',
        current: 1,
        total: ''
      },
    }
  },
  onLoad(options) {
    // this.refreshData()
  },
  onReachBottom(e) {
    console.log("onReachBottom", e)
    this.loadMore()
  },
  methods: {
    getApprovalList() {
      let leave_type = this.selectTabValue, page = this.loadingObj.current
      engine.getApprovalList({page, leave_type}).then(res => {
        this.approvalList = this.approvalList.concat(res.data)
        this.loadFinish(res.total)
      })
    },
    tabClick(item) {
      this.selectTabValue = item.value
      this.refreshData()
    },
    getItemTag(item) {
      if (item.complete_status === 10) {
        return {class: "uncompleted", text: "未完成"}
      } else if (item.status === 0) {
        return {class: '', text: '未审核'}
      } else if (item.status === 1) {
        return {class: "checked", text: '已审核'}
      }
    },
    toBreakDetail(item) {
      let leave_type = this.selectTabValue
      this.$u.route({url: breakDetail, params: {...item, leave_type}})
    },
    refreshData() {
      this.loadingObj.current = 1
      this.loadingObj.status = ''
      this.approvalList = []
      this.getApprovalList()
    },
    loadMore() {
      if (this.loadingObj.current === this.loadingObj.total) {
        this.loadingObj.status = 'nomore'
      } else {
        this.loadingObj.status = 'loading'
        this.loadingObj.current = this.loadingObj.current + 1
        this.getApprovalList()
      }
    },
    loadFinish(total) {
      this.loadingObj.total = Math.ceil(total / 10)
      this.loadingObj.status = ''
      if (this.loadingObj.current >= this.loadingObj.total
          && this.approvalList.length > 0) {
        this.loadingObj.status = 'nomore'
      }
    },
  }
}
</script>

<style>
page {
  /*background: #F2F2F2;*/
}

.top-tabs {
  z-index: 100;
  position: fixed;
  width: 100%;
  height: 37px;
  background: #FFFFFF;
  border-bottom: 1px solid #F2F2F2;
}

.tabs-view {
  flex: 1;
  line-height: 37px;
  text-align: center;
  /* 文本样式 */
  font-size: 12px;
  font-family: PingFang SC;
  font-weight: 400;
}

.tabs-view.select {
  color: #42B4AB;
  border-bottom: 1px solid #42B4AB;
}

.homework-list {
  margin-top: 37px;
}

.ask-break {
  width: 100%;
  margin-bottom: 20px;
}

.ask-break.top {
  margin-top: 21px;
}

.ask-break-icon {
  width: 42px;
  height: 42px;
  margin-left: 12px;
  margin-right: 12px;
  flex-shrink: 0;
}

.approval-layout {
  flex: 1;
  width: 100%;
}

.approval-layout > .title {
  font-size: 12px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #333333;
}

.approval-infos {
  margin-top: 6px;
  padding: 15px;
  width: calc(100% - 34px);
  background: #D8F8F5;
  border-radius: 12px;
}

.approval-infos > * {
  line-height: 16px;
  font-size: 16px;
  color: #FD9033;
}

.approval-infos > .title {
  margin-top: 13px;
  margin-bottom: 15px;
  font-size: 14px;
  line-height: 14px;
  font-weight: bold;
  color: #333333;
}

.approval-infos > .detail {
  margin-top: 6px;
  color: #A8A08C;
  width: 100%;
}

.detail > .text {
  flex: 1;
  line-height: 14px;
  font-size: 14px;
  color: #333333;
}
</style>
